// bouncefilter.h

#ifndef BOUNCEFILTER_H
#define BOUNCEFILTER_H

#include "filter.h"

class BounceFilter : public Filter<bool> {
public:
	BounceFilter(bool init, int delayOn) 
		: value_(init), delayOn_(delayOn), delayOff_(delayOn), cnt_(0)
	{}

	BounceFilter(bool init, int delayOn, int delayOff) 
		: value_(init), delayOn_(delayOn), delayOff_(delayOff), cnt_(0)
	{}
	
	virtual ~BounceFilter() {}

	virtual bool update(bool value) {
		if (value_ != value && cnt_ < (value ? delayOn_ : delayOff_))
			++cnt_;
		else {
			cnt_ = 0;
			value_ = value;
		}
		return value_;
	}

	virtual bool operator()() {
		return value_;
	}

	virtual void set(bool value) {
		value_ = value;
		cnt_ = 0;
	}
	
private:
	bool value_;
	const int delayOn_;
	const int delayOff_;	
	int cnt_;
};


#endif
